# plugs/mylist.py
#
#

""" maintain lists """

from gozerbot.persist import Persist
from gozerbot.datadir import datadir
from gozerbot.commands import cmnds
from gozerbot.plughelp import plughelp
from gozerbot.examples import examples
from gozerbot.users import users

plughelp.add('mylist', 'maintain lists based on username')

mylists = Persist(datadir + '/mylists')
if not mylists.data:
    mylists.data = {}

def addtolist(username, listname, item):
    if not mylists.data.has_key(username):
        mylists.data[username] = {}
    if not mylists.data[username].has_key(listname):
        mylists.data[username][listname] = []
    mylists.data[username][listname].append(item)
    mylists.save()

def delfromlist(username, listname, listofnrs):
    try:
        mylist = mylists.data[username][listname]
    except KeyError:
        return -1
    itemsdeleted = 0
    for i in range(len(mylist)-1, -1 , -1):
        if i in listofnrs:
            try:
                del mylist[i]
                itemsdeleted += 1
            except IndexError:
                pass
    if itemsdeleted:
        mylists.save()
    return itemsdeleted

def handle_mylist(bot, ievent):
    """ list <listname> [',' <item>] """
    if not ievent.rest:
        ievent.missing("<listname> [',' <item>]")
        return
    username = users.getname(ievent.userhost)
    try:
        listname, item = ievent.rest.split(',', 1)
    except ValueError:
        teller = 0
        resultstr = ""
        listname = ievent.rest.strip()
        try:
            mylist = mylists.data[username][listname]
        except KeyError:
            ievent.reply('no %s list available' % listname)
            return
        for i in mylist:
            resultstr += "%s) %s " % (teller, i)
            teller += 1
        ievent.reply(resultstr)
        return
    listname = listname.strip().lower()
    item = item.strip()
    if not listname or not item:
        ievent.missing("<listname> [',' <item>]")
        return
    addtolist(username, listname, item)
    ievent.reply('%s added to %s list' % (item, listname))

cmnds.add('mylist', handle_mylist, 'USER')
examples.add('mylist', "mylist <listname> [',' <item>] .. show content of \
list or add item to list", '1) mylist bla 2) mylist bla, mekker')

def handle_mylistdel(bot, ievent):
    """ mylist-del <listname> ',' <listofnrs> .. remove items with indexnr 
        from list """
    if not ievent.rest:
        ievent.missing('<listname>, <listofnrs>')
        return
    try:
        listname, listofnrs = ievent.rest.split(',', 1)
    except ValueError:
        ievent.missing('<listname>, <listofnrs>')
        return
    try:
        nrs = []
        for i in listofnrs.split():
            nrs.append(int(i))
    except ValueError:
        ievent.reply('%s is not an integer' % i)
        return
    username = users.getname(ievent.userhost)
    listname = listname.strip().lower()
    if not mylists.data.has_key(username):
        ievent.reply('no lists available for' % username)
        return
    if not mylists.data[username].has_key(listname):
        ievent.reply('no %s list available for %s' % (listname, username))
        return
    nrs.sort()
    nrs.reverse()
    nrdel = delfromlist(username, listname, nrs)
    if nrdel == -1:
        ievent.reply('error deleting items from %s' % listname)
        return
    if len(mylists.data[username][listname]) == 0:
        del mylists.data[username][listname]
        ievent.reply('%s list removed' % listname)
        return
    ievent.reply('%s item(s) removed from %s list' % (nrdel, listname))

cmnds.add('mylist-del', handle_mylistdel, 'USER')
examples.add('mylist-del', "mylist-del <listname> ',' <listofnrs> .. remove \
items with indexnr from list", '1) mylist-del mekker , 1 2) mylist-del \
mekker , 0 3 6')

def handle_mylists(bot, ievent):
    """ show avaiable lists """
    username = users.getname(ievent.userhost)
    if not mylists.data.has_key(username):
        ievent.reply('no lists available for %s' % username)
        return
    l = mylists.data[username].keys()
    if not l:
        ievent.reply('no lists available for %s' % username)
        return
    else:
        ievent.reply(' .. '.join(l))

cmnds.add('mylists', handle_mylists, 'USER')
examples.add('mylists', 'show available lists' , 'mylists')
